package van.planifolia.volatiles;

import java.util.concurrent.TimeUnit;

/**
 * Created by Intellij IDEA<br>
 *
 * @author Planifolia.Van
 * @version 1.0
 * @date 2022/11/8 16:24
 */
public class VolatileDemo {
    public static void main(String[] args) throws InterruptedException {

        TestThread testThread = new TestThread();
        new Thread(testThread,"线程a").start();
        TimeUnit.SECONDS.sleep(3);
        testThread.flag=false;
    }
}
class TestThread implements Runnable{
    public volatile boolean flag = true;
    @Override
    public void run() {
        while (true){
            if (!flag){
                System.out.println(Thread.currentThread().getName()+" 终止了");
                break;
            }
        }
    }
}